package com.mt.course.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.course.entity.CourseCategoryEntity;
import com.mt.course.mapper.CourseCategoryEntityMapper;

@Component
public class CourseCategoryServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CourseCategoryServiceModel.class);

    @Autowired
    private CourseCategoryEntityMapper courseCategoryEntityMapper;

    public ServiceResult<List<CourseCategoryEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<List<CourseCategoryEntity>> result = new ServiceResult<List<CourseCategoryEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.courseCategoryEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<CourseCategoryEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<CourseCategoryEntity> result = new ServiceResult<CourseCategoryEntity>();
        try {
            result.setResult(this.courseCategoryEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(CourseCategoryEntity courseCategoryEntity) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.courseCategoryEntityMapper.insert(courseCategoryEntity);
            if (save > 0) {
                id = courseCategoryEntity.getCourseCategoryId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(CourseCategoryEntity courseCategoryEntity) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.courseCategoryEntityMapper.updateByPrimaryKeySelective(courseCategoryEntity);
            if (save > 0) {
                id = courseCategoryEntity.getCourseCategoryId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(CourseCategoryEntity courseCategoryEntity) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.courseCategoryEntityMapper.updateByPrimaryKeySelective(courseCategoryEntity);
            if (save > 0) {
                id = courseCategoryEntity.getCourseCategoryId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer courseCategoryId) {
        Assert.notNull(this.courseCategoryEntityMapper, "Property 'courseCategoryEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.courseCategoryEntityMapper.deleteByPrimaryKey(courseCategoryId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CourseCategoryServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
